package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.SocketCommand;
	import com.wandian.data.GameData;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.avatar.ContextMenuVersion;
	import com.wandian.view.components.ui.avatar.model.AvatarModel;
	import com.wandian.view.components.ui.avatar.model.Param;
	import com.wandian.view.components.ui.avatar.view.CutView;
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.system.Capabilities;
	import flash.utils.ByteArray;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * @author long
	 * @Create  2012-12-16 下午03:39:42
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class HeadUploadMediator extends Mediator
	{
		public static const NAME:String="HeadUploadMediator";
		public var parameter:Object;
		public var initImgURL:String;
		public var uid:String;
		public var cutView:CutView;
		public var avatarModel:AvatarModel;
		public var tempUploadURL:String;
		private var swfStage:Stage;
		private var currentHead:ByteArray;
		public function HeadUploadMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}

		override public function onRegister():void
		{
			this.mcHeadUpload.name="mcHeadUpload";
			this.swfStage=this.stage;
//			this.swfStage.align = StageAlign.TOP_LEFT;
//			this.swfStage.scaleMode = StageScaleMode.NO_SCALE;
			this.mcHeadUpload.contextMenu=ContextMenuVersion.version("www.soche8.com", "11.07.05");
			this.parameter=this.mcHeadUpload.loaderInfo.parameters;
			/*
			用AS2时，可以直接把参数加在flash的尾部，如:demo.swf?u1=good&u2=bad
			在flash里就默认u1,u2为根变量。可以使用_root.u1和_root.u2来得到值
			但到了AS3里面这样做已经不行了。
			要用到flash.display.loaderInfo类的parameters属性，返回的是一个参数对象
			使用方法:
			例如在文档类中定义var param:Object = root.loaderInfo.parameters;
			如果取u1的值，可以用param["u1"],同样u2的值：param["u2"]
			*/
			Param.s=this.swfStage;
			Param.uid=this.parameter["uid"];
			// 默认图片地址
			Param.pSize=this.parameter["pSize"] ? (this.parameter["pSize"]) : '300|300|200|200|100|100|50|50';
			Param.pSize=(Param.pSize).split("|");
			for (var i in Param.pSize)
			{
				Param.pSize[i]=int(Param.pSize[i]);
			}

			Param.imgUrl=this.parameter["imgUrl"] ? (this.parameter["imgUrl"]) : ("1_120.jpg");
			Param.uploadUrl=this.parameter["tmpurl"] ? (this.parameter["tmpurl"]) : ("http://localhost/flex/text2/upload.php");
			Param.tmpImgUrl='http://localhost/tmpImgUrl';
			Param.jsFunc=this.parameter["jsfunc"];
			Param.jsLang=this.parameter["jslang"];
			Param.initLanguage();
			this.avatarModel=new AvatarModel();
//			this.avatarModel.loaderPic(Param.imgUrl);
			this.cutView=new CutView(this.avatarModel);
			this.mcHeadUpload.addChild(this.cutView);
			this.cutView.x=54.8;
			this.cutView.y=42.5;

			facade.registerCommand(CLZNoticeName.UPDATE_PERSON_HEAD_REQ, SocketCommand);
			this.mcHeadUpload.title_txt.text=GameData.tip["UPDATE_UPLOADHEADER_TITLE"];
			AddBtn.initBtn(this.mcHeadUpload, CLZNoticeName.HIDE_UPLOADHEADER_PANEL, AddBtn.CLOSE_TWO, "", 4, 1);
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.HIDE_UPLOADHEADER_PANEL, CLZNoticeName.UPDATE_PERSON_HEAD_RES, CLZNoticeName.UPDATE_PERSON_HEAD_REQ];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=notification.getBody();
			switch (notification.getName())
			{
				//关闭上传头像
				case CLZNoticeName.HIDE_UPLOADHEADER_PANEL:
				{
					if (Texas.I._mcWindosView.contains(this.mcHeadUpload))
					{
						Texas.I._mcWindosView.removeChildTo(this.mcHeadUpload);
						viewComponent=null;
					}
					facade.removeCommand(CLZNoticeName.UPDATE_PERSON_HEAD_REQ);
					facade.removeMediator(HeadUploadMediator.NAME);
					break;
				}
				case CLZNoticeName.UPDATE_PERSON_HEAD_REQ:
				{
					currentHead=notification.getBody() as ByteArray;
					break;
				}
				case CLZNoticeName.UPDATE_PERSON_HEAD_RES:
				{
					this.sendNotification(CLZNoticeName.HIDE_UPLOADHEADER_PANEL);
					var tipData:Object=notification.getBody();
					if (tipData.errId == 0)
					{
						//this._global.userInfo.userVo.icon=tipData.ico;
						this.sendNotification(CLZNoticeName.UPDATE_PERSON_HEAD_SUC,this.currentHead);
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["PERSON_UPDATE_SUCC"]});
					}
					else
					{
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["PERSON_UPDATE_ERR"]});
					}
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get stage():Stage
		{
			return mcHeadUpload.parent.stage;
		}

		private function get mcHeadUpload():MovieClip
		{
			return viewComponent as MovieClip;
		}
	}
}
